package com.hinnotek.amoeba.ui.activity.old

import android.os.Bundle
import android.view.View
import androidx.activity.viewModels
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.lee.common.base.BaseActivity
import com.hinnotek.amoeba.R
import com.hinnotek.amoeba.adapters.old.ImageAdapter
import com.hinnotek.amoeba.adapters.old.WipeOutDetailAdapter
import com.hinnotek.amoeba.base.BaseBean
import com.hinnotek.amoeba.bean.old.AmibaInstallByIdBean
import com.hinnotek.amoeba.databinding.ActivityMsgWipeOutDetailBinding
import com.hinnotek.amoeba.dialog.CustomDialog
import com.hinnotek.amoeba.utilities.IntentUtils
import com.hinnotek.amoeba.utilities.ToastUtils
import com.hinnotek.amoeba.viewmodels.NoticeViewModel
import com.hinnotek.amoeba.dialog.RejectCustomDialog
import com.lee.common.network.net.ObserverState
import dagger.hilt.android.AndroidEntryPoint
import java.util.*

@AndroidEntryPoint
class MsgWipeOutDetailActivity : BaseActivity<ActivityMsgWipeOutDetailBinding>(),
    View.OnClickListener {
    private var adapter: WipeOutDetailAdapter? = null
    private var mAdapter: ImageAdapter? = null
    private var amibaReimburseId = 0
    private var noticeId = 0
    private var applyUserId = 0
    private var approvalUserId: String? = null

    private val viewModel by viewModels<NoticeViewModel>()


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setLeftVisible(true)
        setContentView(R.layout.activity_msg_wipe_out_detail)
        setTitle("报销申请")

    }

    override fun initVew() {
        bindingView.superButtonHistory.setOnClickListener(this)
        bindingView.superButtonReject.setOnClickListener(this)
        bindingView.superButtonAgree.setOnClickListener(this)
        val manager = LinearLayoutManager(mContext, RecyclerView.VERTICAL, false)
        bindingView.rlvWoDetails.layoutManager = manager
        bindingView.rlvWoDetails.adapter = WipeOutDetailAdapter().also { adapter = it }
        val manager1 = GridLayoutManager(mContext, 3, RecyclerView.VERTICAL, false)
        bindingView.rlvWoEvidence.layoutManager = manager1
        bindingView.rlvWoEvidence.adapter = ImageAdapter().also { mAdapter = it }
    }

    override fun setData() {
        val type = intent.getIntExtra("type", 0)
        noticeId = intent.getIntExtra("noticeId", 0)
        if (type == 0) {
            bindingView.llMsgBtns.visibility = View.VISIBLE
        } else if (type == 1) {
            bindingView.llMsgBtns.visibility = View.GONE
        } else {
            bindingView.superButtonReject.visibility = View.GONE
            bindingView.superButtonAgree.visibility = View.GONE
        }
        getNoticeDetailById(noticeId)
    }

    override fun onClick(v: View) {
        when (v.id) {
            R.id.superButton_history -> {
                val bundle = Bundle()
                bundle.putInt("msgType", 4)
                bundle.putInt("applyUserId", applyUserId)
                bundle.putString("approvalUserId", approvalUserId)
                IntentUtils.startActivity(mContext, MsgHistoryListActivity::class.java, bundle)
            }
            R.id.superButton_reject -> {
                val rejectCustomDialog =
                    RejectCustomDialog(mContext)
                rejectCustomDialog.setOnClickBottomListener(object :
                    RejectCustomDialog.OnClickBottomListener {
                    override fun onPositiveClick() {
                        amibaReimburseApproval(
                            rejectCustomDialog, amibaReimburseId,
                            3, noticeId, rejectCustomDialog.editText
                        )
                    }

                    override fun onNegtiveClick() {
                        rejectCustomDialog.dismiss()
                    }
                })
                rejectCustomDialog.setCanceledOnTouchOutside(false)
                rejectCustomDialog.show()
            }
            R.id.superButton_agree -> amibaReimburseApproval(
                null, amibaReimburseId, 2, noticeId, ""
            )
        }
    }

    /**
     * app端通过id查询单个临时缴费续费申请接口
     */
    fun getNoticeDetailById(noticeId: Int) {
        val customDialog = CustomDialog(mContext, "正在加载...")
        customDialog.show()

        viewModel.getNoticeDetailById(noticeId)
            .observe(this, object : ObserverState<AmibaInstallByIdBean>() {
                override fun onDataChange(data: AmibaInstallByIdBean) {
                    customDialog.dismiss()
                    applyUserId = data.applyUserId
                    approvalUserId = data.approvalUserId
                    amibaReimburseId = data.amibaReimburseId
                    bindingView.tvWoType.text = if (data.amibaReimburseType == 1) "产品" else "项目"
                    if (data.amibaReimburseType == 1) {
                        bindingView.llWoChooseUnit.visibility = View.GONE
                        bindingView.tvWoChooseProject.text = data.provideProjectName
                    } else {
                        bindingView.llWoChooseUnit.visibility = View.VISIBLE
                        bindingView.tvWoChooseUnit.text = data.customerName
                        bindingView.tvWoChooseProject.text = data.projectName
                    }
                    bindingView.tvApplicant.text = data.createUser
                    bindingView.tvRelatedApprover.text = data.approvalUser
                    if (data.approvalState == 1) {
                        bindingView.tvApproverStatus.text = "已申请"
                        bindingView.tvApproverStatus.setTextColor(getColor21(R.color.black))
                    } else if (data.approvalState == 2) {
                        bindingView.tvApproverStatus.text = "已同意"
                        bindingView.tvApproverStatus.setTextColor(getColor21(R.color.xui_btn_green_normal_color))
                    } else if (data.approvalState == 3) {
                        bindingView.tvApproverStatus.text = "已拒绝"
                        bindingView.tvApproverStatus.setTextColor(getColor21(R.color.xui_config_color_red))
                        bindingView.llReject.visibility = View.VISIBLE
                        bindingView.tvReject.text = data.refusalReasons
                    } else if (data.approvalState == 4) {
                        bindingView.tvApplicantStatus.text = "已撤回"
                        bindingView.tvApplicantStatus.setTextColor(getColor21(R.color.xui_config_color_red))
                    }
                    adapter?.setList(data.amibaInstallContinueds)
                    val list1: MutableList<String> = ArrayList()
                    for (amibaReimbursePictures in data.amibaReimbursePictures) {
                        list1.add(amibaReimbursePictures.pictureUrl)
                    }
                    mAdapter?.setList(list1)
                    bindingView.tvWoCc.setText(data.sendUser)
                    super.onDataChange(data)
                }
                override fun onError(msg: String?, code: Int) {
                    customDialog.dismiss()
                    ToastUtils.showShort(mContext, msg)
                    super.onError(msg, code)
                }
            })

    }

    /**
     * app端阿米巴报销申请审批接口
     */
    fun amibaReimburseApproval(
        dialog: RejectCustomDialog?, amibaReimburseId: Int, approvalState: Int,
        noticeId: Int, refusalReasons: String
    ) {
        val customDialog = CustomDialog(mContext, "正在加载...")
        customDialog.show()
        viewModel.getAmibaReimburseApproval(amibaReimburseId,approvalState,noticeId,refusalReasons).observe(this,object : ObserverState<BaseBean>(){
            override fun onDataChange(data: BaseBean) {
                customDialog.dismiss()
                dialog?.dismiss()
                finishActivity()
                super.onDataChange(data)
            }
            override fun onError(msg: String?, code: Int) {
                customDialog.dismiss()
                dialog?.dismiss()
                ToastUtils.showShort(mContext, msg)
                super.onError(msg, code)
            }
        })
    }
}